package com.ruoyi.durian.domain.utils;

import com.ruoyi.durian.domain.vo.CalculateTheDistanceVo;
import org.springframework.data.geo.Distance;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 计算两点之间的距离以及何时到达
 */
public class CalculateTheDistanceVoUtlis {

    /**
     * 计算两点之间的距离
     *
     * @param distance
     * @return
     */
    public static CalculateTheDistanceVo calculateTheDistance(Distance distance){
        /*
         * 一辆车正常车速是 40km/h
         * 40 / 60m = 0.67km/m
         * 也就是一分钟走0.67km
         * 所以使用计算的直线距离乘以0.67
         */

        BigDecimal time = BigDecimal.valueOf(distance.getValue())
                .divide(new BigDecimal("0.01"))
                .setScale(0, RoundingMode.CEILING);
        //当前时间往后推需要到达的时间
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDateTime advancedTime = LocalDateTime.from(currentTime.plus(Duration.ofSeconds(Long.valueOf(time.toString()))));
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        CalculateTheDistanceVo calculateTheDistanceVo = new CalculateTheDistanceVo();
        calculateTheDistanceVo.setRang(new BigDecimal(distance.getValue()).setScale(2, RoundingMode.CEILING));
        calculateTheDistanceVo.setAdvancedTime(advancedTime.format(pattern));
        return calculateTheDistanceVo;
    }

}
